﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.XmlDataModel;
using System.Xml;
using SharedLib.Helpers;
using System.IO;

namespace Tracy.DataModel
{
    public class MediaFile : XmlDataModelWithAttributesAndID
    {
        public const string KEY_PREVIOUS_PATH = "previous-path";
        public const string KEY_DOWNLOAD_PATH = "download-path";
        public const string KEY_PREVIOUS_STATUS = "previous-status";
        public MediaFile(XmlNode node) : base(node) { if (String.IsNullOrEmpty(Status)) Status = "New"; }
        public MediaFile() : base() { Status = "New"; }

        public override string RootNodeName
        {
            get
            {
                return "media-file";
            }
        }

        /// <summary>
        /// File Name of the MediaFile
        /// </summary>
        public string FileName
        {
            get { return Properties["file-name"]; }
            set { Properties["file-name"] = value; }
        }

        public string GetFileExtension()
        {
            if (String.IsNullOrEmpty(FileName)) return String.Empty;
            int index = FileName.LastIndexOf(".");
            if (index == -1) return String.Empty;
            return FileName.Substring(index + 1).ToLower();
        }

        /// <summary>
        /// Relative Location of the MediaFile
        /// </summary>
        public string Location
        {
            get 
            {
                return Properties["location"]; 
            }
            set { Properties["location"] = value; }
        }

        /// <summary>
        /// Media File Status, could be "New", "In Progress", "Copying","Moving", "Ready", "Watched", "Missing"
        /// </summary>
        public string Status
        {
            get { return Attributes["status"]; }
            set { Attributes["status"] = value; }
        }
        
        public string Visible
        {
            get { return Attributes["visible"]; }
            set { Attributes["visible"] = value; }
        }

        public string CompleteDate
        {
            get { return Attributes["complete-date"]; }
            set { Attributes["complete-date"] = value; }
        }

        private Resource _resource;

        public Resource Resource
        {
            get { return _resource; }
            set
            {
                Properties["resource-id"] = value.ID;
                _resource = value;
            }
        }
        protected override void ParseRootNode(XmlElement rootNode)
        {
            base.ParseRootNode(rootNode);

            string resID = XmlHelper.GetNodeValue(rootNode, "resource-id");
            if (!String.IsNullOrEmpty(resID)) Resource = TracyFacade.Instance.ResourceProvider.GetItemByID(resID);
        }

        public static bool IsVisible(string fileName) //TODO: use config file
        {
            if (fileName.EndsWith(".avi") ||
               fileName.EndsWith(".rmvb") ||
               fileName.EndsWith(".rm") ||
               fileName.EndsWith(".mp4") ||
               fileName.EndsWith(".wmv") ||
               fileName.EndsWith(".mkv")) return true;
            return false;
        }

        public static bool WillDownload(string fileName) //TODO: use config file
        {
            if (fileName.EndsWith(".url") || fileName.StartsWith("_____padding_file")) return false; //Remove AD
            return true;
        }

        public string GetDownloadPath()
        {
            if (Properties.ContainsKey(MediaFile.KEY_DOWNLOAD_PATH))
            {
                if (String.IsNullOrEmpty(Location)) return Path.Combine(Properties[MediaFile.KEY_DOWNLOAD_PATH], FileName);
                return Path.Combine(Properties[MediaFile.KEY_DOWNLOAD_PATH], Location, FileName);
            }
            return null;
        }

        public string GetTempFilePath()
        {
            if (Properties.ContainsKey(MediaFile.KEY_PREVIOUS_PATH))
            {
                if(String.IsNullOrEmpty(Location)) return Path.Combine(Properties[MediaFile.KEY_PREVIOUS_PATH], FileName);
                return Path.Combine(Properties[MediaFile.KEY_PREVIOUS_PATH], Location, FileName);
            }
            return GetDownloadPath();
            //return Path.Combine(defaultDownloadFolder, Location, FileName); //TODO: what if default download folder changes

        }

        //public string GetDownloadingFilePath(string defaultDownloadFolder)
        //{
        //    if (Resource == null) return null;
        //    string fullPath = GetTempFilePath(defaultDownloadFolder);
        //    return (Resource.GetResourceType() == "BT") ? fullPath + ".bt.td" : fullPath + ".td"; //TODO: support other download tool

        //}

        public string GetSavedFilePath()
        {
            if (Resource == null || Resource.Entry == null) return null;
            if (String.IsNullOrEmpty(Location)) return Path.Combine(Resource.Entry.SavePath, FileName);
            return Path.Combine(Resource.Entry.SavePath, Location, FileName);
        }
    }
}
